package com.example.neusoft215gdh.androidpro_21.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft215gdh.androidpro_21.R;
import com.example.neusoft215gdh.androidpro_21.Service.PxService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * Created by hp-pc on 2017/10/12.
 */
public class PayActivity extends AppCompatActivity{

    private TextView peixunname, xunlianleixing, Price, num, bmxuzhi, zongjia;
    private EditText rename, telphone;
    private ImageView jiahao, jianhao;
    private Button jiesuan;
    private ImageButton back;
    private CheckBox weixin, zhifubao;
    private String Peixunname, bmnotice, Userid, trainID;

    private String bmPrice;
    private int shuliang_number = 1;
    private int shulian = 1;
    private double Zhongjia;
    private int paytype = 1;
    String userId;

    Intent intent=getIntent();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.train_pay);
        initView();
        SharedPreferences user=getSharedPreferences("Userid",0);
        userId=user.getString("user_id","");

        //获取总价
        num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(count > 0){
                    int str = Integer.valueOf(num.getText().toString());
                    double  suum = str * Integer.valueOf(bmPrice);
                    Zhongjia=suum;
                    zongjia.setText("合计："+Zhongjia+"元");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String Relname=rename. getText().toString();
                String Telephone=telphone.getText().toString();
                if(Relname.equals("")||Telephone.equals(""))
                {
                    Toast.makeText(PayActivity.this, "请输入有效的真实姓名和联系方式", Toast.LENGTH_SHORT).show();
                    Toast.makeText(PayActivity.this, "报名失败", Toast.LENGTH_SHORT).show();
                }
                else{

                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://172.24.10.175/workout/api.php/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    PxService paySeverice =retrofit.create(PxService.class);

                    Call<String> listCall = paySeverice.pay(Relname,trainID,Telephone,shuliang_number,paytype,Zhongjia,trainID,"191",userId);

                    listCall.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            response.body();
                            Toast.makeText(PayActivity.this, "报名成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            Toast.makeText(PayActivity.this, "报名失败", Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }
        });
    }

    public void initView() {
        //绑到控件
        peixunname = (TextView) findViewById(R.id.peixunName);
        xunlianleixing = (TextView) findViewById(R.id.xunlianleixing);
        Price = (TextView) findViewById(R.id.price);
        num = (TextView) findViewById(R.id.number);
        bmxuzhi = (TextView) findViewById(R.id.bmxuzhi);
        zongjia = (TextView) findViewById(R.id.num);
        rename = (EditText) findViewById(R.id.rename);
        telphone = (EditText) findViewById(R.id.telphone);
        jiahao = (ImageView) findViewById(R.id.jiahao);
        jianhao = (ImageView) findViewById(R.id.jianhao);
        jiesuan = (Button) findViewById(R.id.button_jiesuan);
        weixin = (CheckBox) findViewById(R.id.weixin);
        zhifubao = (CheckBox) findViewById(R.id.zhifubao);
        back = (ImageButton) findViewById(R.id.pay_back);

        //获取传入的值
        Peixunname = getIntent().getStringExtra("peixunmane");
        bmPrice = getIntent().getStringExtra("bmPrice");
        bmnotice = getIntent().getStringExtra("bmnotice");
        Userid = getIntent().getStringExtra("userid");
        trainID = getIntent().getStringExtra("Train_id");
        jiesuan= (Button) findViewById(R.id.button_jiesuan);


        peixunname.setText(Peixunname);
        Price.setText(bmPrice + "元/人");
        bmxuzhi.setText(bmnotice);
        xunlianleixing.setText(Peixunname);
        num.setText(shuliang_number + "");
        Zhongjia = Integer.valueOf(bmPrice) * shuliang_number;
        zongjia.setText("合计:" + Zhongjia + "元");
        //加减号的监听
        shuliang();
        //支付选择
        pay();
    }


    //字符方式的选择
    private void pay() {
        weixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b == true) {
                    paytype = 1;
                    Toast.makeText(PayActivity.this,
                            "你选择了微信支付", Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    /**
     * 加号减号的监听
     * 高德翰
     */
    private void shuliang(){
        jiahao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shulian+=1;
                if(shulian==30)
                {
                    shulian=1;
                }
                shuliang_number = shulian;
                num.setText(shuliang_number+"");
            }
        });

        jianhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(shulian>=1)
                {
                    shulian--;
                    shuliang_number = shulian;
                    num.setText(shuliang_number+"");
                }
            }
        });
    }
}

